/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JPanel;
import picasa.ViewImage;

/**
 *
 * @author Tin Pham
 */
public class Image extends JPanel implements MouseListener{

    private BufferedImage image;
    private String filePath;
    public Image(int x, int y, int width, int height) {
       this.setSize(width, height);
       this.setLocation(x, y);
       this.addMouseListener(this);
    }

    public Image(String filePath, int x, int y, int width, int height) {
        this(x,y,width, height);
        FileInputStream fin = null;
        try {
            BufferedImage[] images;
            this.filePath = filePath;
            fin = new FileInputStream(filePath);
            String suffix = filePath.substring(filePath.lastIndexOf('.') + 1);
            //System.out.println("suf " + suffix);
            Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
            ImageReader imageReader = (ImageReader) readers.next();
            ImageInputStream iis = ImageIO.createImageInputStream(fin);
            imageReader.setInput(iis, false);
            int num = imageReader.getNumImages(true);
            images = new BufferedImage[num];
            for (int j = 0; j < num; ++j) {
                images[j] = imageReader.read(j);
              
            }
            fin.close();
            this.image = images[0];
        } catch (IOException ex) {
            Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
        }  finally {
            try {
                fin.close();
            } catch (IOException ex) {
                Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }

//    public void draw(Graphics g) {
//        g.drawImage(getImage(), getX(), getY(), getWidth(), getHeight(), null);
//        image.flush();
//        System.gc();
//    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int height = this.getHeight();
        int width = this.getWidth();
        g.drawImage(image, 0, 0, width, height, null);
    }
    
    /**
     * @return the image
     */
    public BufferedImage getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(BufferedImage image) {
        this.image = image;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if(e.getClickCount() == 2) {
            try {
                System.out.println(this.filePath);
                ViewImage viewImage = new ViewImage();
                Image i = (Image) this.clone();
                i.setSize(viewImage.getSize());
                i.setLocation(0, 0);
                viewImage.add(i);
                viewImage.setVisible(true);
            } catch (CloneNotSupportedException ex) {
                Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

    @Override
    protected Image clone() throws CloneNotSupportedException {
        return new Image(filePath, getX(), getY(), getWidth(), getHeight());
    }
    
    

}
